
from copy import deepcopy

import past
from flask import Blueprint, request, jsonify
from controllers.common import *
from controllers.db import *

gui_zhen_api = Blueprint('gui_zhen_api', __name__)
gui_zhen_down_api = Blueprint('gui_zhen_down_api', __name__)

@gui_zhen_api.route('/gui_zhen_action', methods=['GET', 'POST'])
def gui_zhen_action():
  params = json.loads(request.data)
  
 
  _action = params['action'] 
  methods={
    'add_data': addMethod,
    'delete_data': deleteMethod,
    'clone_data': cloneMethod,
    'update_method': updateMethod,
    'update_list_method': updateListMethod,
    'get_item_method': getItemMethod,
    'get_list_method': getListMethod,
  }
  # 获取并执行策略action，执行返回值code为0则表示正常
  action = methods.get(_action, nullMethod)
  result = action(params) 
  
  if result['code'] != 0:
    return {'status': result['code'], 'info': result['info']}
  result_data = {'status': 200, 'data': result['data'], 'info': 'request success!'}
  return jsonify(result_data)

@gui_zhen_down_api.route('/gui_zhen_down_action/<int:is_push>', methods=['GET', 'POST'])
def gui_zhen_action(is_push):
  # params = json.loads(request.data)
  # is_push表示导出还是发布
  params = {
    "is_push": is_push
  }
  
  _action = 'push_issue'
  methods={
    'create_json': createJson,
    'push_issue': pushIssue,
  }
  # 获取并执行策略aciotn
  action = methods.get(_action, nullMethod)
  result = action(params)  
  
  # 直接返回push结果、不用特殊额外的处理
  return result


def nullMethod(param):
  return {'code': -1, 'data': '', 'info': '空方法'}


def addMethod(param):
  return {'code': -1, 'data': '', 'info': '方法未实现'}
  

def deleteMethod(param):
  dbObj = GuiZhen.query.filter_by(project=param['project']).first()
  db.session.delete(dbObj)
  db.session.commit()
  projectData = GuiZhen.query.filter_by(project=param['project']).first()
  if projectData is not None:
    return {'code': -2, 'data': '', 'info': 'projectData is not None,删除未成功'}
  else:
    return {'code': 0, 'data': '', 'info': '删除成功'}

def cloneMethod(param):
  data = GuiZhen.query.filter_by(project=param['project']).first()
  cloneObj = GuiZhen(
		remark = data.remark,
		basic = data.basic,
		version = data.version,

    )
  db.session.add(cloneObj)
  db.session.commit()
  return {'code': 0, 'data': '', 'info': 'clone success'}


def updateMethod(param):
  project = param['project'] 
  item = param['data']
  if 'remark' in item:
    GuiZhen.query.filter_by(project=project).update({
      'remark' : json.dumps(item['remark'])
      })
    db.session.commit()
  if 'basic' in item:
    GuiZhen.query.filter_by(project=project).update({
      'basic' : json.dumps(item['basic'])
      })
    db.session.commit()
  if 'version' in item:
    GuiZhen.query.filter_by(project=project).update({
      'version' : int(item['version'])
      })
    db.session.commit()

  return {'code': 0, 'data': '', 'info': 'update success'}

def updateListMethod(param):
  pass

def getItemMethod(param):
  que = GuiZhen.query.filter_by(project=param['project']).first()
  if que is None:
    return {'code': -2, 'data': '', 'info': 'clone success'}

  tmp = {
    "project" : int(que.project),
	"remark" : json.loads(que.remark),
	"basic" : json.loads(que.basic),
	"version" : int(que.version),
  }
  return {'code': 0, 'data': tmp, 'info': 'update success'}

def getListMethod(param):
  data = GuiZhen.query.order_by("project").all()
  result = [] 

  for que in data:
    tmp = {
      "project" : int(que.project),
	  "remark" : json.loads(que.remark),
	  #"basic" : json.loads(que.basic),
	  #"version" : int(que.version),
    }
    result.append(tmp)
  return {'code': 0, 'data': result, 'info': 'update success'}


def createJson(param):
  pass

def pushIssue(param):
  is_push = param['is_push']
  list = GuiZhen.query.order_by("project").all()
  projs = {}
  for que in list:
    tmp = {
      "project" : int(que.project),
	  "remark" : json.loads(que.remark),
	  "basic" : json.loads(que.basic),
	  "version" : int(que.version),
    }
    projs[que.project] = tmp
  return DownLoad('gui_zhen', projs, is_push)



